package com.useeinfo.oa.open.api.wuHanCommon;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.AutomaticTableBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WarningCreateAcceptBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.biz.WuHanUnitBiz;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.AutomaticTable;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WarningCreateAccept;
import com.useeinfo.oa.modules.wuHanCommon.base.entity.WuHanUnit;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.MeterReadingEnum;
import com.useeinfo.oa.modules.wuHanCommon.base.enums.PointSystem;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/wuhan/automaticTable")
public class AutomaticTableAPI {

    @Autowired
    private WuHanUnitBiz wuHanUnitBiz;
    @Autowired
    private AutomaticTableBiz automaticTableBiz;
    @Autowired
    private WarningCreateAcceptBiz warningCreateAcceptBiz;


    /**
     * 获取时间(年月日-时分)
     *
     * @return
     */
    @RequestMapping(value = "/getDate")
    public ResultDto getDate() {
        return ResultUtil.success("", warningCreateAcceptBiz.getDate());
    }

    /**
     * 获取机组信息
     *
     * @return
     */
    @RequestMapping(value = "/getMeterReadingList")
    public ResultDto getMeterReadingList() {
        JSONArray rows = new JSONArray();
        MeterReadingEnum[] meterReadingEnums = MeterReadingEnum.values();
        for (MeterReadingEnum meterReadingEnum : meterReadingEnums) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", meterReadingEnum.getName());
            jsonObject.put("value", meterReadingEnum.getCode());
            rows.add(jsonObject);
        }
        return ResultUtil.success(rows);
    }

    /**
     * 获取系统信息
     *
     * @return
     */
    @RequestMapping(value = "/getSystemList")
    public ResultDto getSystemList() {
        JSONArray rows = new JSONArray();
        PointSystem[] pointSystems = PointSystem.values();
        for (PointSystem pointSystem : pointSystems) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", pointSystem.getName());
            jsonObject.put("value", pointSystem.getCode());
            rows.add(jsonObject);
        }
        return ResultUtil.success(rows);
    }

    /**
     * 获取设备信息
     *
     * @return
     */
    @RequestMapping(value = "/getUnitList")
    public ResultDto getUnitList(String meterReadingEnum) {
        if (StringUtils.isBlank(meterReadingEnum)) {
            return ResultUtil.error("机组不能为空");
        }
        JSONArray rows = new JSONArray();
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("meterReading", meterReadingEnum);
        List<WuHanUnit> wuHanUnits = wuHanUnitBiz.findList(queryParam);
        if (CollectionUtils.isNotEmpty(wuHanUnits)) {
            wuHanUnits.forEach(wuHanUnit -> rows.add(wuHanUnit.toJSONObject()));
        }
        return ResultUtil.success(rows);
    }


    /**
     * 获取设备信息
     *
     * @return
     */
    @RequestMapping(value = "/getUnitListNew")
    public ResultDto getUnitListNew(String meterReadingEnum) {
        if (StringUtils.isBlank(meterReadingEnum)) {
            return ResultUtil.error("系统不能为空");
        }
        JSONArray rows = new JSONArray();
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("pointSystem", meterReadingEnum);
        List<WuHanUnit> wuHanUnits = wuHanUnitBiz.findList(queryParam);
        if (CollectionUtils.isNotEmpty(wuHanUnits)) {
            wuHanUnits.forEach(wuHanUnit -> rows.add(wuHanUnit.toJSONObjectSystem()));
        }
        return ResultUtil.success(rows);
    }

    /**
     * 获取抄表数据
     */
    @RequestMapping("/getAutomaticList")
    public ResultDto getAutomaticList(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                      @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                      @RequestParam(value = "orderBy", required = false) String orderBy,
                                      @RequestParam(value = "meterReading", required = false) String meterReading,
                                      @RequestParam(value = "unitId", required = false) String unitId,
                                      @RequestParam(value = "startDate", required = false) String startDate,
                                      @RequestParam(value = "endDate", required = false) String endDate) {
        if (StringUtils.isBlank(meterReading) && StringUtils.isBlank(unitId)) {
            return ResultUtil.success(new JSONArray());
        }
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("meterReading", meterReading);
        queryParam.getSqlMap().put("d_startDate", startDate);
        queryParam.getSqlMap().put("d_endDate", endDate);
        queryParam.getSqlMap().put("unitId", unitId);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return ResultUtil.success(automaticTableBiz.findJSONList(queryParam));
    }


    /**
     * 获取抄表数据
     */
    @RequestMapping("/getAutomaticListNew")
    public ResultDto getAutomaticListNew(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                         @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                         @RequestParam(value = "orderBy", required = false) String orderBy,
                                         @RequestParam(value = "meterReading", required = false) String meterReading,
                                         @RequestParam(value = "unitId", required = false) String unitId,
                                         @RequestParam(value = "startDate", required = false) String startDate,
                                         @RequestParam(value = "endDate", required = false) String endDate) {
        if (StringUtils.isBlank(meterReading) && StringUtils.isBlank(unitId)) {
            return ResultUtil.success(new JSONArray());
        }
        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        queryParam.getSqlMap().put("pointSystem", meterReading);
        queryParam.getSqlMap().put("startDate", DateTimeUtils.formatDateToStringWithTime(DateTimeUtils.addMinute(new Date(), -10)));
        queryParam.getSqlMap().put("endDate", DateTimeUtils.formatDateToStringWithTime(new Date()));
        queryParam.getSqlMap().put("unitId", unitId);
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        if (automaticTableBiz.totalRecord(queryParam.getSqlMap()) <= 0) {
            queryParam.setPageSize(10);
            queryParam.getSqlMap().put("startDate", DateTimeUtils.formatDateToStringWithTime(DateTimeUtils.getBeginOfDay(new Date())));
        }
        return ResultUtil.success(automaticTableBiz.findJSONList(queryParam));
    }
}
